import { prisma } from '../src/db'

/**
 * 初始化产品配置数据
 * 基于原有的硬编码映射关系创建初始配置
 */
async function initProductConfigs() {
  try {
    console.log('🚀 开始初始化产品配置数据...')

    // 检查是否已有数据
    const existingCount = await prisma.productConfig.count()
    if (existingCount > 0) {
      console.log(`⚠️ 数据库中已存在 ${existingCount} 条产品配置，跳过初始化`)
      return
    }

    // 初始产品配置数据（50K改为0.5M）
    const initialConfigs = [
      {
        coinAmount: 0.5,
        productId: 15,
        description: '0.5M金币',
        sortOrder: 1,
        isActive: true
      },
      {
        coinAmount: 2,
        productId: 16,
        description: '2M金币',
        sortOrder: 2,
        isActive: true
      },
      {
        coinAmount: 5,
        productId: 38,
        description: '5M金币',
        sortOrder: 3,
        isActive: true
      },
      {
        coinAmount: 10,
        productId: 17,
        description: '10M金币',
        sortOrder: 4,
        isActive: true
      },
      {
        coinAmount: 20,
        productId: 39,
        description: '20M金币',
        sortOrder: 5,
        isActive: true
      },
      {
        coinAmount: 40,
        productId: 40,
        description: '40M金币',
        sortOrder: 6,
        isActive: true
      },
      {
        coinAmount: 50,
        productId: 41,
        description: '50M金币',
        sortOrder: 7,
        isActive: true
      },
      {
        coinAmount: 55,
        productId: 19,
        description: '55M金币',
        sortOrder: 8,
        isActive: true
      },
      {
        coinAmount: 100,
        productId: 18,
        description: '100M金币',
        sortOrder: 9,
        isActive: true
      },
      {
        coinAmount: 350,
        productId: 20,
        description: '350M金币',
        sortOrder: 10,
        isActive: true
      }
    ]

    console.log(`📝 准备插入 ${initialConfigs.length} 条产品配置...`)

    // 批量插入数据
    for (const config of initialConfigs) {
      await prisma.productConfig.create({
        data: config
      })
      console.log(`✅ 已创建产品配置: ${config.description} (${config.coinAmount}M -> ID:${config.productId})`)
    }

    console.log('🎉 产品配置数据初始化完成!')

    // 验证插入的数据
    const totalCount = await prisma.productConfig.count()
    const activeCount = await prisma.productConfig.count({ where: { isActive: true } })

    console.log(`📊 统计信息:`)
    console.log(`   总配置数: ${totalCount}`)
    console.log(`   启用配置: ${activeCount}`)

  } catch (error) {
    console.error('❌ 初始化产品配置失败:', error)
    process.exit(1)
  } finally {
    await prisma.$disconnect()
  }
}

// 运行初始化
initProductConfigs().catch((error) => {
  console.error('脚本执行失败:', error)
  process.exit(1)
})